<?php
/**************************************************************************************************
|
| AudioShare - Audio Sharing Script
| http://www.audiosharescript.com
| webmaster@audiosharescript.com
|
|**************************************************************************************************
|
| By using this software you agree that you have read and acknowledged our End-User License 
| Agreement available at http://www.audiosharescript.com/eula.html and to be bound by it.
|
| Copyright (c) 2010 AudioShareScript.com. All rights reserved.
|**************************************************************************************************/

include("include/config.php");
include("include/functions/import.php");
$thebaseurl = $config['baseurl'];

if ($_SESSION[USERID] != "" && $_SESSION[USERID] >= 0 && is_numeric($_SESSION[USERID]))
{

## GET variables
$getpage = htmlentities(strip_tags($_REQUEST[page]));
$page = escape($getpage);

## delete selected playlists...
$deleteMsg="";
if($_REQUEST[submitdelete]!="")
{
	$playlistIds="";
	$deleteplaylist = $_POST[deleteplaylist];
	$playlistCount = count($deleteplaylist);
	$playlistIds=implode(",",$deleteplaylist);
	if($playlistIds!=""){
		$query = "DELETE FROM playlists WHERE PLAYLISTID in ($playlistIds)";
		$conn->Execute($query);
		
		## remove playlist xml files...
		if($playlistCount>0){
			foreach($deleteplaylist as $key=>$val){
				remove_playlist_xml($val);
			}
		}
		## xml playlist files removed successfully...
		
		$playlistVar="playlist";
		if($playlistCount>1)
			$playlistVar="playlists";
		$deleteMsg=$playlistCount." ".$playlistVar." has been deleted successfully.";
	}
}	
##  Playlists deleted...

## decide page from here...
if($page=="")
{
	$page = "1";
}
$currentpage = $page;
if ($page >=2)
{
	$pagingstart = ($page-1)*$config[items_per_page];
}
else
{
	$pagingstart = "0";
}
## page decided

## get playlists from here...
$query1 = "SELECT count(*) as total from playlists where active='1' AND USERID='$_SESSION[USERID]' $sort limit $config[maximum_results]";
$query2 = "SELECT * from playlists where active='1' AND USERID='$_SESSION[USERID]' $sort limit $pagingstart, $config[items_per_page]";
$showtitle = $lang['801'];

STemplate::assign('showtitle',$showtitle);
STemplate::assign('pagetitle',$showtitle);

$executequery1 = $conn->Execute($query1);

$totalplaylists = $executequery1->fields['total'];

if ($totalplaylists > 0)
{

	if($executequery1->fields['total']<=$config[maximum_results])
	{
	$total = $executequery1->fields['total'];
	}
	else
	{
	$total = $config[maximum_results];
	}

	$toppage = ceil($total/$config[items_per_page]);
	if($toppage==0)
	{
	$xpage=$toppage+1;
	}
	else
	{
	$xpage = $toppage;
	}

	$executequery2 = $conn->Execute($query2);

	$listplaylists = $executequery2->getrows();

	$beginning=$pagingstart+1;
	$ending=$pagingstart+$executequery2->recordcount();
	$pagelinks="";
	$k=1;
	$theprevpage=$currentpage-1;
	$thenextpage=$currentpage+1;

	if ($currentpage > 0)
	{
		$pagelinks.='<table width="100%" cellspacing="2" cellpadding="2" border="0">';
		$pagelinks.='<tr>';
			$pagelinks.='<td width="33%">';
			$pagelinks.='<a href="javascript:void(0);"><img height="16" width="16" border="0" align="absmiddle" src="'.$config[imageurl].'/ico_rss_s.gif" alt="RSS"  /></a>'; 
				$pagelinks.='<span class="pag"> page '.$currentpage.' of '.$total.'</span>';
			$pagelinks.='</td>';	
			$pagelinks.='<td width="33%" align="right">';
				$pagelinks.="<div class=\"pagination\">";
				if($currentpage > 1) 
				{
					$pagelinks.="<a href='$thebaseurl/myplaylist.php/1'>$lang[611]</a>";
					$pagelinks.="<span class='ellipsis'>...</span>";
					$pagelinks.="<a href='$thebaseurl/myplaylist.php?page=$theprevpage'>&laquo; $lang[612]</a>";
				}
				else
				{
					$pagelinks.="<span>&lt;</span>";
				}
				$counter=0;
				$lowercount = $currentpage-5;
				if ($lowercount <= 0) $lowercount = 1;
				
				while ($lowercount < $currentpage)
				{
					$pagelinks.="<a href='$thebaseurl/myplaylist.php?page=$lowercount'>$lowercount</a>";
					$lowercount++;
					$counter++;
				}
				
				$pagelinks.="<span class='current'><span>$currentpage</span></span>";
				
				$uppercounter = $currentpage+1;
				
				while (($uppercounter < $currentpage+10-$counter) && ($uppercounter<=$toppage))
				{
					$pagelinks.="<a href='$thebaseurl/myplaylist.php?page=$uppercounter'>$uppercounter</a>";
					$uppercounter++;
				}
				
				if($currentpage < $toppage) 
				{
					$pagelinks.="<a href='$thebaseurl/myplaylist.php?page=$thenextpage'>$lang[613] &raquo;</a>";
					$pagelinks.="<span class='ellipsis'>...</span>";
					$pagelinks.="<a href='$thebaseurl/myplaylist.php?page=$toppage'>$lang[614]</a>";
				}
				else
				{
					$pagelinks.="<span class=\"disable\">&gt;</span>";
				}
				$pagelinks.="</div>";
			$pagelinks.='</td>';	
		$pagelinks.='</tr>';
		$pagelinks.="</table>";
	}
}
else
{
	$error = "You don't have any playlists yet. ";
	STemplate::assign('error',$error);
}
## got the users playlists
}
else
{
	header("Location:$config[baseurl]/login.php");exit;
}
$action=$config["baseurl"]."/myplaylist.php";

//TEMPLATES BEGIN
STemplate::assign('deleteMsg',$deleteMsg);
STemplate::assign('action',$action);
STemplate::assign('error',$error);
STemplate::assign('message',$message);
STemplate::assign('beginning',$beginning);
STemplate::assign('ending',$ending);
STemplate::assign('pagelinks',$pagelinks);
STemplate::assign('total',$total);
STemplate::assign('listplaylists',$listplaylists);
STemplate::display('header.tpl');
STemplate::display('myplaylist.tpl');
STemplate::display('footer.tpl');
//TEMPLATES END
?>